package dianmobile.eistarandroid.ui.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.Item;
import dianmobile.eistarandroid.beans.PostsIntroduction;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.ui.activities.UserInfoActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;

public class BoardContentListAdapter extends BaseAdapter{

    /** list item view data */
    private List<Object> valueList = null;

    private Context context;
    private LayoutInflater inflater;

    /*send to UserInfoActivity*/
    private String userName;

    /**
     * indicate which type of view be show.
     * if equals {@link ConstantsData#ENUM_TOP10_LIST_VIEW}, means the view's type is "十大".
     * if equals {@link ConstantsData#ENUM_RECOMMENDATION_LIST_VIEW}, means the view's type is "订阅".
     */
    private int adaptType = 0;

    /** layout id */
    private static final int LIST_ITEM_LAYOUT = R.layout.listitem_board_content;

    public BoardContentListAdapter(Context context, List<Object> valueList, int adaptType) {
        DataUtils.notNull(valueList);
        DataUtils.notNull(context);

        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.adaptType = adaptType;
        this.valueList = valueList;
    }

    public void setValueList(List<Object> valueList) {
        this.valueList = valueList;
    }

//    public BoardContentListAdapter(Context context, List<PostsIntroduction> top10ListViewData) {
//        this.context = context;
//        this.inflater = LayoutInflater.from(context);
//        this.adaptType = ConstantsData.ENUM_TOP10_LIST_VIEW;
//        this.top10ListViewData = top10ListViewData;
//    }
//
//    public BoardContentListAdapter(Context context, List<Item> recommendationListViewData) {
//        this.context = context;
//        this.inflater = LayoutInflater.from(context);
//        this.adaptType = ConstantsData.ENUM_TOP10_LIST_VIEW;
//        this.recommendationListViewData = recommendationListViewData;
//    }

    @Override
    public int getCount() {
        return valueList.size();
    }

    @Override
    public Object getItem(int position) {
        return valueList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(LIST_ITEM_LAYOUT, null);
            viewHolder = new ViewHolder();
            viewHolder.userHeadIv = (CircleImageView) convertView.findViewById(R.id.iv_user_head);
            viewHolder.titleTv = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.authorTv = (TextView) convertView.findViewById(R.id.tv_author);
            viewHolder.replyCountTv = (TextView) convertView.findViewById(R.id.tv_reply_count);
            viewHolder.replyCountIv = (ImageView) convertView.findViewById(R.id.iv_reply_count);
            viewHolder.summaryTv = (TextView) convertView.findViewById(R.id.tv_summary);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        configureConvertView(convertView, position, viewHolder);
//        viewHolder.userHeadIv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, UserInfoActivity.class);
//                intent.putExtra(ConstantsData.EXTRA_USER_NAME, userName);
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    private void getUserHeadFromNetwork(ImageView imageView, String imageName ){
        String url = DataUtils.getUserHeadUrlFromName(imageName);
        RequestFactory.makeNetworkImage(imageView, url, R.drawable.default_face, 0);
    }

    /**
     * According to the value of {@link #adaptType}, to configure the display view's data.
     * @param view
     * @param position
     */
    private void configureConvertView(View view, final int position, ViewHolder viewHolder) {

        switch (adaptType) {
            // 十大
            case ConstantsData.ENUM_TOP10_LIST_VIEW:
                final PostsIntroduction postsIntroduction = (PostsIntroduction) valueList.get(position);
//                final PostsIntroduction postsIntroduction = top10ListViewData.get(position);
                //userName = postsIntroduction.getAuthor();

                viewHolder.titleTv.setText(postsIntroduction.getTitle());
                viewHolder.authorTv.setText(postsIntroduction.getAuthor());
                viewHolder.replyCountTv.setText(postsIntroduction.getReplyCount());
                viewHolder.summaryTv.setText(postsIntroduction.getSummary());
                getUserHeadFromNetwork(viewHolder.userHeadIv, postsIntroduction.getAuthor());

                viewHolder.userHeadIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra(ConstantsData.EXTRA_USER_NAME, postsIntroduction.getAuthor());
                        context.startActivity(intent);
                    }
                });
                break;

            // 订阅
            case ConstantsData.ENUM_RECOMMENDATION_LIST_VIEW:
                final Item item = (Item) valueList.get(position);
//                final Item item = recommendationListViewData.get(position);
                //userName = item.getAuthor();

                viewHolder.titleTv.setText(item.getTitle());
                viewHolder.authorTv.setText(item.getAuthor());
                viewHolder.replyCountTv.setVisibility(View.INVISIBLE);
                viewHolder.replyCountIv.setVisibility(View.INVISIBLE);
                viewHolder.summaryTv.setText(item.getSummary());
                getUserHeadFromNetwork(viewHolder.userHeadIv, item.getAuthor());

                viewHolder.userHeadIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra(ConstantsData.EXTRA_USER_NAME, item.getAuthor());
                        context.startActivity(intent);
                    }
                });
                break;
            default:
                break;
        }
    }

    class ViewHolder {
        CircleImageView userHeadIv;
        TextView titleTv;
        TextView authorTv;
        TextView replyCountTv;
        ImageView replyCountIv;
        TextView summaryTv;
    }
}
